package com.lida.zwservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lida.commonuntils.R;
import com.lida.zwservice.entity.*;
import com.lida.zwservice.service.PersonalDescriptionService;
import com.lida.zwservice.service.PersonalDetailedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 账务表 前端控制器
 * </p>
 *
 * @author LiDa
 * @since 2022-01-20
 */
@RestController
@RequestMapping("/zwservice/detailed")
@CrossOrigin
public class PersonalDetailedController {

    @Autowired
    private PersonalDetailedService personal;

    @Autowired
    private PersonalDescriptionService description;

    //添加账务
//    @PostMapping("addDetailed")
//    public R add(@RequestBody PersonalDetailed personalDetailed){
//        personal.save(personalDetailed);
//        return R.success();
//    }
    @PostMapping("addDetailed")
    public R add(@RequestBody AddDetailedVo addDetailedVo){
        PersonalDetailed personalDetailed =new PersonalDetailed();

        personalDetailed.setTitle(addDetailedVo.getTitle());
        personalDetailed.setNum(addDetailedVo.getNum());
        personalDetailed.setType(addDetailedVo.getType());
        personalDetailed.setSortId(addDetailedVo.getSortId());
        personalDetailed.setGmtCreate(addDetailedVo.getGmtCreate());

        personal.save(personalDetailed);

        PersonalDescription personalDescription=new PersonalDescription();

        personalDescription.setDescription(addDetailedVo.getDescription());
        personalDescription.setId(personalDetailed.getId());
        personalDescription.setGmtCreate(addDetailedVo.getGmtCreate());

        description.save(personalDescription);

        return R.success();
    }

    //删除账务
    @DeleteMapping("delete/{id}")
    public R delete (@PathVariable String id){
        personal.removeById(id);
        return R.success();
    }
    //更新账务
    @PostMapping("update")
    public R update(@RequestBody PersonalDetailed personalDetailed){
        personal.updateById(personalDetailed);
        return R.success();
    }
    //查询所有账务
//    @GetMapping("findAll")
//    public R findAll(){
//        List<PersonalDetailed> list = personal.list(null);
//        return R.success().data("list",list);
//    }
    //分页查询所有账务
    @GetMapping("findAll/{page}/{limit}")
    public R findAll(@PathVariable long page,
                     @PathVariable long limit){
        Page<PersonalDetailed> list =new Page<>(page,limit);
        QueryWrapper<PersonalDetailed> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        personal.page(list,wrapper);
        List<PersonalDetailed> item =list.getRecords();
        long total =list.getTotal();
        return R.success().data("item",item).data("total",total);
    }

    //条件分页查询账务列表
    @PostMapping("pageCondition/{page}/{limit}")
    public R pageCondition (@PathVariable long page, @PathVariable long limit,
                            @RequestBody (required=false)DetailedVo detailedVo){
        Page<PersonalDetailed> pageList =new Page<>(page,limit);
        QueryWrapper<PersonalDetailed> wrapper =new QueryWrapper<>();

        String type =detailedVo.getType();
        String title =detailedVo.getTitle();
        String num =detailedVo.getNum();

        List time=detailedVo.getTime();
        String begin="";
        String end="";
        if(time.size()!=0){
            begin=time.get(0).toString();
            end =time.get(1).toString();
        }

        if(!StringUtils.isEmpty(type)){
            wrapper.eq("type",type);
        }
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(num)){
            wrapper.eq("num",num);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        personal.page(pageList,wrapper);
        long total =pageList.getTotal();
        List<PersonalDetailed> item =pageList.getRecords();

        return R.success().data("records",item).data("total",total);
    }

    //修改账务信息，回显
    @GetMapping("getDetailedById/{id}")
    public R getDetailedById(@PathVariable String id){
        GetDetailedByIdVo detailed=personal.getDetailedById(id);
        return R.success().data("datailed",detailed);
    }

    //Excel模块调用，生成目标集合
    @PostMapping("writeExcel")
    public List<PersonalDetailed> writeExcel (@RequestBody (required=false)ExcelDetailedVo detailedVo){
        List<PersonalDetailed> items=personal.selectItems(detailedVo);
        return items;
    }

    //Excel模块调用，分页查询
    @PostMapping("getExcelList/{page}/{limit}")
    public R getExcelList(@PathVariable long page,@PathVariable long limit,
                           @RequestBody ExcelDetailedVo excelDetailed){
        Page<PersonalDetailed> list=new Page<>(page,limit);
        String title=excelDetailed.getTitle();
        String max=excelDetailed.getMax();
        String min=excelDetailed.getMin();
        String type=excelDetailed.getType();

        List time=excelDetailed.getTime();
        String begin="";
        String end="";
        if(time.size()!=0){
            begin=time.get(0).toString();
            end =time.get(1).toString();
        }

        QueryWrapper<PersonalDetailed> wrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(max)){
            wrapper.le("num",Integer.valueOf(max));
        }
        if(!StringUtils.isEmpty(min)){
            wrapper.ge("num",Integer.valueOf(min));
        }
        if(!StringUtils.isEmpty(type)){
            wrapper.eq("type",type);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        wrapper.orderByDesc("gmt_create");
        personal.page(list,wrapper);
        List items=list.getRecords();
        long total=list.getTotal();

        return R.success().data("records",items).data("total",total);
    }

}

